package statistics;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;

public class WriteStatistics {
	
	private File file; 
	
	public WriteStatistics(File file){
		this.file = file; 
	}
	
	public void writeStatistics(List<PopulationStatistics> statistics){
		PrintWriter writer = null;
		File parent = file.getParentFile();
		if (!parent.exists()) {
			parent.mkdirs(); 
		}
		try {
			writer = new PrintWriter(file);
			
			writer.println("best,avg,SD"); 
			for (PopulationStatistics generation : statistics){
				writer.println(getGenerationString(generation)); 
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			writer.flush(); 
			writer.close(); 
		}
	}
	
	private String getGenerationString(PopulationStatistics generation){
		String theString = "";
		theString += generation.getBestFitness() + ","; 
		theString += generation.getAvgFitness() + ","; 
		theString += generation.getStandardDiviation() + ",";
		return theString; 
	}

}
